package com.tianyang.yukuaifuzs.business.trade.presenter;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tianyang.yukuaifuzs.business.trade.bean.SubMerchantStatOutput;
import com.tianyang.yukuaifuzs.business.trade.contract.SubMerchantStatContract;
import com.tianyang.yukuaifuzs.business.trade.model.TradeModel;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：商户详情统计
 * Created by xyj on 2024/3/26
 */
public class SubMerchantStatPresenter implements SubMerchantStatContract.Presenter {

    private SubMerchantStatContract.View mView;

    private CompositeSubscription mSubscription;

    private String phone;

    public SubMerchantStatPresenter(SubMerchantStatContract.View mView, String phone) {
        this.mView = mView;
        this.phone = phone;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void posMerchantTradeDetail(int pageNumber, String startTime, String endTime) {
        Subscription subscription = TradeModel.getInstance().posMerchantTradeDetail(pageNumber, startTime, endTime, phone)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showMerchantTradeListView(null, pageNumber == CommonConfig.PAGE_NUM);
                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                    }

                    @Override
                    public void onNext(JSONObject outputs) {
                        List<SubMerchantStatOutput> recordOutputs = null;
                        int totalPage = 0;
                        if (outputs != null) {
                            recordOutputs = outputs.getObject("list", new TypeReference<List<SubMerchantStatOutput>>(){});
                            mView.showMerchantTradeStatView(outputs.getJSONObject("obj"));
                            totalPage = outputs.getIntValue("totalPage");
                        }
                        mView.showMerchantTradeListView(recordOutputs, pageNumber == CommonConfig.PAGE_NUM);
                        if (pageNumber < totalPage) {
                            mView.showLoadMore(CommonConfig.LOADMORECOMPLETE);
                        } else {
                            mView.showLoadMore(CommonConfig.LOADMOREEND);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
